<?php
class TicketDao {
	public $table = 'ticket';
	public function get($id){
		return ClassManager::getSql()->oneDataCache($this->table,'id', intval($id));
	}
	public function add($data){
		$field = array('number','password','add_time','add_admin','valid_start_time','valid_end_time','product_id','verifier');
		ClassManager::getSql()->insertArray($this->table, $field, $data);
	}
	public function searchList($where=array(),$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}
	public function searchData($where = array()){
		return ClassManager::getSql()->onedata1($this->table,'*',$where);
	}
	
	public function searchNumber($where){
		$where = $this->searchWhere($where);
		$tmp = ClassManager::getSql()->onedata1($this->table,"count(*)  ",$where);
		return $tmp[0];
	}
	
	
	public function searchMutilNumber($where){
		$where = $this->searchMutilWhere($where);
		$sql = "select count(*) cnt from eo_ticket a left join eo_ticket_log b on a.id=b.ticket_id where 1=1 ";
		$sql.= $where;
		$tmp = ClassManager::getSql()->dataMutillist('ticket and ticket_log cnt',$sql);
		return $tmp[0]['cnt'];
	}

	public function searchMutilList($where=array(),$order,$limit=''){
		$where = $this->searchMutilWhere($where);
		$sql = " select a.*,b.ticket_id,b.add_time,b.use_user,b.use_user_id,b.use_time,";
		$sql .= " b.activation_time,b.activation_admin,b.cancellation_time,b.cancellation_admin,";
		$sql .= " b.send_admin,b.send_time,b.order_id";
		$sql .= " from eo_ticket a left join eo_ticket_log b on a.id=b.ticket_id where 1=1 ";
		$sql.= $where;
		return ClassManager::getSql()->dataMutillist('ticket and ticket_log ',$sql,$limit);
	}
	
	public function searchMutilWhere($where){
		$where1 = '';
		if(isset($where['ticket_send_type'])&&$where['ticket_send_type']==TICKET_SENT){
			$where1 .=" and b.send_time != '0'";
		}
		if(isset($where['ticket_send_type'])&&$where['ticket_send_type']==TICKET_NOSEND){
			$where1 .=" and b.send_time = '0'";
			
		}
		if(isset($where['ticket_used_type'])&&$where['ticket_used_type']==TICKET_USED){
			$where1 .=" and b.use_time != '0'";
		}
		
		if(isset($where['ticket_used_type'])&&$where['ticket_used_type']==TICKET_NOUSED){
			$where1 .=" and b.use_time = '0'";
		}

		if(isset($where['product_id'])&&$where['product_id']>0){
			$where1 .=" and a.product_id = '".intval($where['product_id'])."'";
		}

		if(isset($where['number'])&&$where['number']!=""){
			$where1 .=" and a.number like '%".$where['number']."%'";
		}
		
		if(isset($where['start_number'])&&$where['start_number']!=""){
			$where1 .=" and a.number >='".$where['start_number']."'";
		}
		if(isset($where['end_number'])&&$where['end_number']!=""){
			$where1 .=" and a.number <='".$where['end_number']."'";
		}

		if(isset($where['status'])&&$where['status']!=""){
			$where1 .=" and a.status ='".$where['status']."'";
		}
		
		if(isset($where['receive_user_id'])&&$where['receive_user_id']>0){
			$where1 .=" and a.receive_user_id ='".intval($where['receive_user_id'])."'";
		}


		if(isset($where['use_user_id'])&&$where['use_user_id']>0){
			$where1 .=" and b.use_user_id ='".intval($where['use_user_id'])."'";
		}
		
		return $where1;
	}
	
	public function searchWhere($where){
		$where1 = array();
		$likeWhere = array('number');
		foreach ($where as  $k=>$v){
			if (isset($v)&&$v!==""){
				if (in_array($k,$likeWhere) and !is_array($v)){
					$where1[$k] = array('%'.$v.'%' , 'like');
				}else{
					$where1[$k] =  $v;
				}
			}
		}
		return $where1;
	}
		
	public function updates($data , $id){
		ClassManager::getSql()->updates($this->table, $data,"where id in(".$id.")");
	}
	public function updateById($data , $id){
		ClassManager::getSql()->updates($this->table, $data,"where id=".intval($id));
	}
	public function getByNumber($number){
		return ClassManager::getSql()->onedata($this->table,'*' , "where number in (".$number.") ");
	}
	public function getByNumberList($number){
		return ClassManager::getSql()->datalist($this->table,'*',"where number in (".$number.") ");
	}
	public function getVoucher($where = array()){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->onedata1($this->table,'*',$where);
	}
}
?>